package com.msunsoft.common;

import java.io.Serializable;

public class Page implements Serializable {

    private int     showCount                = 10;                        // 每页显示记录数
    private int     totalPage;                                            // 总页数
    private int     totalResult;                                          // 总记录数
    private int     currentPage;                                          // 当前页
    private int     currentResult;                                        // 当前记录起始索引
    private boolean entityOrField;                                        // true:需要分页的地方，传入的参数就是Page实体；false:需要分页的地方，传入的参数所代表的实体拥有Page属性
    private String  pageStr;                                              // 最终页面显示的底部翻页导航，详细见：getPageStr();
    private String  id;                                                   // 如果想以ajax方式去分页的id
    private String  form;                                                 // 以哪个form的action进行分页
    private String  upPageStr;                                            // 上一页
    private String  downPageStr;                                          // 下一页
    // 以下三个属性，防止同一页面加载多个分页，js错误
    private String  callBackFunction;                                     // nextPage() javascript方法回调函数的javascript名称，此方法暂不支持参数
    private String  nextPage                 = "nextPage";                // javascript方法 nextPage的方法名
    private String  hunterPageContrlNextJump = "hunterPageContrlNextJump";// javascript方法 hunterPageContrlNextJump的方法名
    // xuchegli 2015年4月18日10:00:17 增加排序功能
    private String  sortName;
    private String  sortOrder;

    public String getSortName(){
        return sortName;
    }

    public void setSortName(String sortName){
        this.sortName = sortName;
    }

    public String getSortOrder(){
        return sortOrder;
    }

    public void setSortOrder(String sortOrder){
        this.sortOrder = sortOrder;
    }

    public String getHunterPageContrlNextJump(){
        return hunterPageContrlNextJump;
    }

    public void setHunterPageContrlNextJump(String hunterPageContrlNextJump){
        this.hunterPageContrlNextJump = hunterPageContrlNextJump;
    }

    public String getNextPage(){
        return nextPage;
    }

    public void setNextPage(String nextPage){
        this.nextPage = nextPage;
    }

    public String getCallBackFunction(){
        return callBackFunction;
    }

    public void setCallBackFunction(String callBackFunction){
        this.callBackFunction = callBackFunction;
    }

    public int getShowCount(){
        return showCount;
    }

    public void setShowCount(Integer object){
        this.showCount = object;
    }

    public String getId(){
        return id;
    }

    public void setId(String id){
        this.id = id;
    }

    public String getForm(){
        return form;
    }

    public void setForm(String form){
        this.form = form;
    }

    public int getTotalPage(){
        if (totalResult % showCount == 0) totalPage = totalResult / showCount;
        else totalPage = totalResult / showCount + 1;
        return totalPage;
    }

    public void setTotalPage(int totalPage){
        this.totalPage = totalPage;
    }

    public int getTotalResult(){
        return totalResult;
    }

    public void setTotalResult(int totalResult){
        this.totalResult = totalResult;
    }

    public int getCurrentPage(){
        if (currentPage <= 0) currentPage = 1;
        if (currentPage > getTotalPage ()) currentPage = getTotalPage ();
        return currentPage;
    }

    public void setCurrentPage(int currentPage){
        this.currentPage = currentPage;
    }

    public String getPageStr(){
        StringBuilder sb = new StringBuilder ();
        if (totalResult > 0) {
            sb.append ("	<ul>\n");
            sb.append ("<li >总数：" + totalResult + "</li>\n");
            if (currentPage == 1) {
                sb.append ("	<li class=\"pageinfo\">首页</li>\n");
                sb.append ("	<li class=\"pageinfo\">上页</li>\n");
            } else {
                sb.append ("	<li><a href=\"javascript:void(0)\"  onclick=\"" + nextPage + "(1)\">首页</a></li>\n");
                sb.append ("	<li><a href=\"javascript:void(0)\"  onclick=\"" + nextPage + "(" + (currentPage - 1) + ")\">上页</a></li>\n");
            }
            int showTag = 5; // 分页标签显示数量
            int startTag = 1;
            int endTag;
            if (currentPage <= 3) {
                startTag = 1;
                endTag = showTag;
            } else {
                startTag = currentPage - 2;
                endTag = startTag + showTag - 1;
            }
            for ( int i = startTag ; i <= totalPage && i <= endTag ; i++ ) {
                if (currentPage == i) sb.append ("<li class=\"current\">" + i + "</li>\n");
                else sb.append ("	<li><a href=\"javascript:void(0)\"    onclick=\"" + nextPage + "(" + i + ")\">" + i + "</a></li>\n");
            }
            if (currentPage == totalPage) {
                sb.append ("	<li class=\"pageinfo\">下页</li>\n");
                sb.append ("	<li class=\"pageinfo\">尾页</li>\n");
            } else {
                sb.append ("	<li><a href=\"javascript:void(0)\"  onclick=\"" + nextPage + "(" + (currentPage + 1) + ")\">下页</a></li>\n");
                sb.append ("	<li><a href=\"javascript:void(0)\"  onclick=\"" + nextPage + "(" + totalPage + ")\">尾页</a></li>\n");
            }
            // sb.append(" <li class=\"pageinfo\">第"+currentPage+"页</li>\n");

            sb.append ("	<li class=\"pageinfo\">共" + totalPage + "页</li>\n");
            sb.append (" <li ><a href=\"javascript:void(0)\"  onclick=\"" + hunterPageContrlNextJump + "()\" style=\"width:50px;\">跳转到</a> </li>\n");
            sb.append (" <li ><input type='text' id='num' size='1'style=\"border:1px solid \" value=\"" + currentPage + "\"/> </li>\n");
            sb.append (" <input type='hidden' id='all' value='" + totalPage + "'/> \n");
            sb.append ("</ul>\n");

            sb.append ("<script type=\"text/javascript\">\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("$.ajaxSetup({cache:false});\n");
            }
            sb.append ("function " + nextPage + "(page){\n");

            if (form != null && !"".equals (form.trim ())) {
                sb.append ("		var forms=$(\"#" + form + "\");\n");
                sb.append ("	if(true && forms){\n");
            } else {
                sb.append ("	if(true && document.forms[0]){\n");
            }
            // 修正在不用ajax下分页问题 lishenjie by 2012-6-16
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("		var url = forms.attr('action');\n");
                // sb.append(" alert(url);\n");
            } else {
                sb.append ("		var url = document.forms[0].getAttribute(\"action\");\n");
            }
            sb.append ("		if(url.indexOf('?')>-1){url += \"&" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            sb.append ("		else{url += \"?" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            if (id != null && !"".equals (id.trim ())) {
                sb.append (" 	url+=page+\"&status=ajax\";\n");
                sb.append (" 	$(\"#" + id + "\").load(url,$('#" + form + "').serializeArray(),function(){");
                if (callBackFunction != null) {
                    sb.append ("if(typeof(" + callBackFunction + ")=='function'){" + callBackFunction + "();}");
                }
                sb.append ("});\n");
            } else {
                // sb.append("alert(url+page);");
                sb.append ("		document.forms[0].action = url+page;\n");
                // sb.append(" alert(\"1111\");\n");
                sb.append ("		document.forms[0].submit();\n");
            }
            sb.append ("	}else{\n");
            if (form != null && !"".equals (form.trim ())) { // 修正（同上）
                sb.append ("		var url = window.location.pathname+window.location.search+'';\n");
            } else {
                // sb.append(" alert(\"233333in\");\n");
                sb.append ("		var url = document.location+'';\n");
            }
            sb.append ("		if(url.indexOf('?')>-1){\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append (" 		if(url.indexOf('#@')>0){\n");
                sb.append (" 	 		url=url.replace('#@','');\n");
                sb.append (" 		}\n");
            }
            sb.append ("			if(url.indexOf('currentPage')>-1){\n");
            sb.append ("				var reg = /currentPage=\\d*/g;\n");
            sb.append ("				url = url.replace(reg,'currentPage=');\n");
            sb.append ("			}else{\n");
            sb.append ("				url += \"&" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";\n");
            sb.append ("			}\n");
            sb.append ("		}else{url += \"?" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            // System.out.println("id的值为："+id);
            if (id != null && !"".equals (id.trim ())) {
                sb.append (" 	url+=page+\"&status=ajax\";\n");
                sb.append (" 	$(\"#" + id + "\").load(url);\n");
            } else {
                sb.append ("		document.location = url + page;\n");
            }
            sb.append ("	}\n");
            sb.append ("}\n");

            // 跳转到
            sb.append (" function " + hunterPageContrlNextJump + "(){\n");
            sb.append ("var jumpPageNumber=document.getElementById('num').value;\n");
            // sb.append("alert(jumpPageNumber);\n");
            sb.append ("if(jumpPageNumber==''){\n");
            sb.append ("alert('未输入页码！');\n");
            sb.append ("" + nextPage + "(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");

            sb.append ("var lastPageNumber=Number(document.getElementById('all').value);\n");
            // sb.append("alert((!Number(jumpPageNumber)));\n");
            sb.append ("if((!Number(jumpPageNumber))){\n");
            sb.append ("alert('输入错误,只能输入数值！');\n");
            sb.append ("" + nextPage + "(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");
            sb.append ("if(Number(jumpPageNumber)<1){\n");
            sb.append ("alert('输入错误,只能输入正数！');\n");
            sb.append ("" + nextPage + "(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");
            // sb.append("alert(Number(lastPageNumber));\n");
            sb.append ("if(Number(jumpPageNumber)<=Number(lastPageNumber)){\n");
            sb.append ("if (Number(jumpPageNumber)==1){\n");
            sb.append ("" + nextPage + "(1);\n");
            sb.append ("}else{\n");
            sb.append ("" + nextPage + "(Number(jumpPageNumber));\n");
            sb.append ("}\n");
            sb.append ("}else{alert('超过最大页数！');\n");
            sb.append ("" + nextPage + "(1);\n ");
            sb.append ("}\n}\n");
            sb.append ("</script>\n");
        }
        pageStr = sb.toString ();
        return pageStr;
    }

    public void setPageStr(String pageStr){
        this.pageStr = pageStr;
    }

    public String getDownPageStr(){
        StringBuilder sb = new StringBuilder ();

        if (totalResult > 0) {
            if (currentPage == totalPage) {
                sb.append ("	<div href=\"#\"></div>\n");
            } else {
                sb.append ("	<div  onclick=\"nextPage(" + (currentPage + 1) + ")\"> <img src=\"/phs/images/hidedownpage.png\" /></div>\n");
            }

            sb.append ("<script type=\"text/javascript\">\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("$.ajaxSetup({cache:false});\n");
            }
            sb.append ("function nextPage(page){\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("		var forms=$(\"#" + form + "\");\n");
                sb.append ("	if(true && forms){\n");
            } else {
                sb.append ("	if(true && document.forms[0]){\n");
            }
            // 修正在不用ajax下分页问题 lishenjie by 2012-6-16
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("		var url = forms.attr('action');\n");
            } else {
                sb.append ("		var url = document.forms[0].getAttribute(\"action\");\n");
            }
            sb.append ("		if(url.indexOf('?')>-1){url += \"&" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            sb.append ("		else{url += \"?" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            if (id != null && !"".equals (id.trim ())) {
                sb.append (" 	url+=page+\"&status=ajax\";\n");
                sb.append (" 	$(\"#" + id + "\").load(url,$('#" + form + "').serializeArray(),function(){});\n");
            } else {
                sb.append ("		document.forms[0].action = url+page;\n");
                sb.append ("		document.forms[0].submit();\n");
            }
            sb.append ("	}else{\n");
            if (form != null && !"".equals (form.trim ())) { // 修正（同上）
                sb.append ("		var url = window.location.pathname+window.location.search+'';\n");
            } else {
                sb.append ("		var url = document.location+'';\n");
            }
            sb.append ("		if(url.indexOf('?')>-1){\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append (" 		if(url.indexOf('#@')>0){\n");
                sb.append (" 	 		url=url.replace('#@','');\n");
                sb.append (" 		}\n");
            }
            sb.append ("			if(url.indexOf('currentPage')>-1){\n");
            sb.append ("				var reg = /currentPage=\\d*/g;\n");
            sb.append ("				url = url.replace(reg,'currentPage=');\n");
            sb.append ("			}else{\n");
            sb.append ("				url += \"&" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";\n");
            sb.append ("			}\n");
            sb.append ("		}else{url += \"?" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            // System.out.println("id的值为："+id);
            if (id != null && !"".equals (id.trim ())) {
                sb.append (" 	url+=page+\"&status=ajax\";\n");
                sb.append (" 	$(\"#" + id + "\").load(url);\n");
            } else {
                sb.append ("		document.location = url + page;\n");
            }
            sb.append ("	}\n");
            sb.append ("}\n");

            // 跳转到
            sb.append (" function hunterPageContrlNextJump(){\n");
            sb.append ("var jumpPageNumber=document.getElementById('num').value;\n");
            // sb.append("alert(jumpPageNumber);\n");
            sb.append ("if(jumpPageNumber==''){\n");
            sb.append ("alert('未输入页码！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");

            sb.append ("var lastPageNumber=Number(document.getElementById('all').value);\n");
            // sb.append("alert((!Number(jumpPageNumber)));\n");
            sb.append ("if((!Number(jumpPageNumber))){\n");
            sb.append ("alert('输入错误,只能输入数值！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");
            sb.append ("if(Number(jumpPageNumber)<1){\n");
            sb.append ("alert('输入错误,只能输入正数！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");
            // sb.append("alert(Number(lastPageNumber));\n");
            sb.append ("if(Number(jumpPageNumber)<=Number(lastPageNumber)){\n");
            sb.append ("if (Number(jumpPageNumber)==1){\n");
            sb.append ("nextPage(1);\n");
            sb.append ("}else{\n");
            sb.append ("nextPage(Number(jumpPageNumber));\n");
            sb.append ("}\n");
            sb.append ("}else{alert('超过最大页数！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("}\n}\n");
            sb.append ("</script>\n");

        }
        upPageStr = sb.toString ();
        return upPageStr;
    }

    public void setUpPageStr(String upPageStr){
        this.upPageStr = upPageStr;
    }

    public String getUpPageStr(){
        StringBuilder sb = new StringBuilder ();

        if (totalResult > 0) {
            if (currentPage == 1) {
                sb.append ("	<div href=\"#\"></div>\n");
            } else {
                sb.append ("	<div  onclick=\"nextPage(" + (currentPage - 1) + ")\"><img src=\"/phs/images/hideuppagepng.png\"/></div>\n");
            }
            sb.append ("<script type=\"text/javascript\">\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("$.ajaxSetup({cache:false});\n");
            }
            sb.append ("function nextPage(page){\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("		var forms=$(\"#" + form + "\");\n");
                sb.append ("	if(true && forms){\n");
            } else {
                sb.append ("	if(true && document.forms[0]){\n");
            }
            // 修正在不用ajax下分页问题 lishenjie by 2012-6-16
            if (form != null && !"".equals (form.trim ())) {
                sb.append ("		var url = forms.attr('action');\n");
                // sb.append(" alert(url);\n");
            } else {
                sb.append ("		var url = document.forms[0].getAttribute(\"action\");\n");
            }
            sb.append ("		if(url.indexOf('?')>-1){url += \"&" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            sb.append ("		else{url += \"?" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            if (id != null && !"".equals (id.trim ())) {
                sb.append (" 	url+=page+\"&status=ajax\";\n");
                sb.append (" 	$(\"#" + id + "\").load(url,$('#" + form + "').serializeArray(),function(){});\n");
            } else {
                // sb.append("alert(url+page);");
                sb.append ("		document.forms[0].action = url+page;\n");
                sb.append ("		document.forms[0].submit();\n");
            }
            sb.append ("	}else{\n");
            if (form != null && !"".equals (form.trim ())) { // 修正（同上）
                sb.append ("		var url = window.location.pathname+window.location.search+'';\n");
            } else {
                sb.append ("		var url = document.location+'';\n");
            }
            sb.append ("		if(url.indexOf('?')>-1){\n");
            if (form != null && !"".equals (form.trim ())) {
                sb.append (" 		if(url.indexOf('#@')>0){\n");
                sb.append (" 	 		url=url.replace('#@','');\n");
                sb.append (" 		}\n");
            }
            sb.append ("			if(url.indexOf('currentPage')>-1){\n");
            sb.append ("				var reg = /currentPage=\\d*/g;\n");
            sb.append ("				url = url.replace(reg,'currentPage=');\n");
            sb.append ("			}else{\n");
            sb.append ("				url += \"&" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";\n");
            sb.append ("			}\n");
            sb.append ("		}else{url += \"?" + (entityOrField ? "currentPage" : "page.currentPage") + "=\";}\n");
            // System.out.println("id的值为："+id);
            if (id != null && !"".equals (id.trim ())) {
                sb.append (" 	url+=page+\"&status=ajax\";\n");
                sb.append (" 	$(\"#" + id + "\").load(url);\n");
            } else {
                sb.append ("		document.location = url + page;\n");
            }
            sb.append ("	}\n");
            sb.append ("}\n");

            // 跳转到
            sb.append (" function hunterPageContrlNextJump(){\n");
            sb.append ("var jumpPageNumber=document.getElementById('num').value;\n");
            // sb.append("alert(jumpPageNumber);\n");
            sb.append ("if(jumpPageNumber==''){\n");
            sb.append ("alert('未输入页码！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");

            sb.append ("var lastPageNumber=Number(document.getElementById('all').value);\n");
            // sb.append("alert((!Number(jumpPageNumber)));\n");
            sb.append ("if((!Number(jumpPageNumber))){\n");
            sb.append ("alert('输入错误,只能输入数值！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");
            sb.append ("if(Number(jumpPageNumber)<1){\n");
            sb.append ("alert('输入错误,只能输入正数！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("return;\n");
            sb.append ("}\n");
            // sb.append("alert(Number(lastPageNumber));\n");
            sb.append ("if(Number(jumpPageNumber)<=Number(lastPageNumber)){\n");
            sb.append ("if (Number(jumpPageNumber)==1){\n");
            sb.append ("nextPage(1);\n");
            sb.append ("}else{\n");
            sb.append ("nextPage(Number(jumpPageNumber));\n");
            sb.append ("}\n");
            sb.append ("}else{alert('超过最大页数！');\n");
            sb.append ("nextPage(1);\n ");
            sb.append ("}\n}\n");
            sb.append ("</script>\n");
        }
        downPageStr = sb.toString ();
        return downPageStr;
    }

    public void setDownPageStr(String downPageStr){
        this.downPageStr = downPageStr;
    }

    public int getCurrentResult(){
        currentResult = (getCurrentPage () - 1) * getShowCount ();
        if (currentResult < 0) currentResult = 0;
        return currentResult;
    }

    public void setCurrentResult(int currentResult){
        this.currentResult = currentResult;
    }

    public boolean isEntityOrField(){
        return entityOrField;
    }

    public void setEntityOrField(boolean entityOrField){
        this.entityOrField = entityOrField;
    }

}
